package com.example.apple.printingroommanagementsystem;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.apple.printingroommanagementsystem.R;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends Activity {
    //private static final String TAG = SplashActivity.class.getSimpleName();//SplashActivity
//    private Handler handler =new Handler();
    ViewPager viewPager;
    LinearLayout linerlayout;
    TextView textview;
    List<ImageView>mylist;
    Button button;
    // 广告图素材
    private int[] bannerImages = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4};
    // 广告语
    private String[] bannerTexts = {"因为专业 所以卓越", "坚持创新 行业领跑", "诚信 专业 双赢", "精细 和谐 大气 开放"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initView();
        initData();
        setUpListening();
        button =(Button) findViewById(R.id.进入主页按钮);
        button.setOnClickListener(new startMainActivity());

   }

    private void setUpListening() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            LinearLayout lin = (LinearLayout)findViewById(R.id.显示框);
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==3){
                    button.setVisibility(View.VISIBLE);
                    lin.setVisibility(View.GONE);
                }else {
                    lin.setVisibility(View.VISIBLE);
                    button.setVisibility(View.GONE);
                }
                textview.setText(bannerTexts[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        mylist = new ArrayList<>();
        for (int i=0;i<bannerImages.length;i++){
            ImageView imageView = new ImageView(SplashActivity.this);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            InputStream is = this.getResources().openRawResource(bannerImages[i]);
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inJustDecodeBounds = false;
            options.inSampleSize = 3; //width，hight设为原来的十分一
            Bitmap btp =BitmapFactory.decodeStream(is,null,options);
            imageView.setImageBitmap(btp);
            mylist.add(imageView);
        }
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mylist.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mylist.get(position));
                return mylist.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mylist.get(position));
            }
        });
        textview.setText(bannerTexts[0]);

    }

    private void initView() {
        viewPager = (ViewPager)findViewById(R.id.splash_viewpager);
        textview = (TextView)findViewById(R.id.tvsplash_text);
        linerlayout = (LinearLayout)findViewById(R.id.points);
    }

    public static Bitmap readBitMap(Context context, int resId){
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
//获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is,null,opt);
    }

    //    @Override
//    public boolean isViewFromObject(View view, Object object) {
//        return view == object;
//    }
//    @Override
//    public Object instantiateItem(ViewGroup container, int position) {
//        container.addView(mList.get(position%mList.size()));
//        return mList.get(position%mList.size());
//    }
//
//    @Override
//    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeView(mList.get(position%mList.size()));
//    }
    //跳转到主页面，并把当前页面关闭
//    private void startMainActivity (){
//        Intent intent = new Intent(this,MainActivity.class);
//        startActivity(intent);
//        finish();
//    }

    private class startMainActivity implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SplashActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
//    //点击屏幕触发事件⌚
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        //
//        Log.e(TAG,"onTouchEvent ==Action"+event.getAction());
//        startMainActivity();
//        return super.onTouchEvent(event);
//    }

//    @Override
//    protected void onDestroy() {
//        //把所有的消息和回调移除
//        handler.removeCallbacksAndMessages(null);
//        super.onDestroy();
//    }
}
